<template>
  <a-card :bordered='false'>

    <!-- 查询区域 -->
    <div class='table-page-search-wrapper'>
      <a-form layout='inline' @keyup.enter.native='searchQuery'>
        <a-row :gutter='24'>
          <a-col :xl='5' :lg='7' :md='8' :sm='24'>
            <a-form-item label='所属项目'>
              <a-select show-search :filter-option='filterOption' v-model='queryParam.projectId' placeholder='请输入所属项目' @change='initUserList'
                        style='margin-right: 20px'>
                <a-select-option value=''>请选择项目</a-select-option>
                <a-select-option v-for='item in projectList' :label='item.name' :value='item.id' :key='item.id'>
                  <span style='float: left'>{{ item.name }}</span>
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl='5' :lg='7' :md='8' :sm='24'>
            <a-form-item label='业主姓名'>
              <a-select show-search :filter-option='filterOption' v-model='queryParam.userName' placeholder='请选择业主姓名'
                        style='margin-right: 20px'>
                <a-select-option value=''>请选择业主</a-select-option>
                <a-select-option v-for='item in userList' :label='item.name' :value='item.id' :key='item.id'>
                  <span style='float: left'>{{ item.userName }}</span>
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl='5' :lg='7' :md='8' :sm='24'>
            <a-form-item label='访客姓名'>
              <a-input placeholder='请输入访客姓名' v-model='queryParam.visitName'></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl='5' :lg='7' :md='8' :sm='24'>
            <a-form-item label='访客手机号'>
              <a-input placeholder='请输入访客手机号' v-model='queryParam.visitMobile'></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl='5' :lg='7' :md='8' :sm='24'>
            <a-form-item label='访客身份证'>
              <a-input placeholder='请输入访客身份证号' v-model='queryParam.cardNum'></a-input>
            </a-form-item>
          </a-col>


          <a-col :md='5' :sm='4'>
            <a-form-item label='授权时间'>
              <a-date-picker showTime format='YYYY-MM-DD HH:mm:ss' placeholder='开始时间' v-model='queryParam.opTime_begin'
                             @change='onChangeStartTime' />
            </a-form-item>
          </a-col>
          <a-col :md='5' :sm='4'>
            <a-form-item>
              <a-date-picker showTime format='YYYY-MM-DD HH:mm:ss' placeholder='结束日期' v-model='queryParam.opTime_end'
                             @change='onChangeEndTime' />
            </a-form-item>
          </a-col>

          <a-col :xl='6' :lg='7' :md='8' :sm='24'>
            <span style='float: left;overflow: hidden;' class='table-page-search-submitButtons'>
              <a-button type='primary' @click='searchQuery' icon='search'>查询</a-button>
              <a-button type='primary' @click='searchReset' icon='reload' style='margin-left: 8px'>重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class='table-operator'>
      <a-button @click='handleAdd' type='primary' icon='plus'>新增</a-button>
      <a-dropdown v-if='selectedRowKeys.length > 0'>
        <a-menu slot='overlay'>
          <a-menu-item key='1' @click='batchDel'>
            <a-icon type='delete' />
            删除
          </a-menu-item>
        </a-menu>
        <a-button style='margin-left: 8px'> 批量操作
          <a-icon type='down' />
        </a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class='ant-alert ant-alert-info' style='margin-bottom: 16px;'>
        <i class='anticon anticon-info-circle ant-alert-icon'></i> 已选择 <a
        style='font-weight: 600'>{{ selectedRowKeys.length }}</a>项
        <a style='margin-left: 24px' @click='onClearSelected'>清空</a>
      </div>

      <a-table
        ref='table'
        size='middle'
        bordered
        rowKey='id'
        :columns='columns'
        :dataSource='dataSource'
        :pagination='ipagination'
        :loading='loading'
        :scroll="{ x: 1000}"
        class='j-table-force-nowrap'
        :rowSelection='{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}'
        @change='handleTableChange'>

        <span slot='action' slot-scope='text, record'>
          <a @click='handleEdit(record)'>编辑</a>

          <a-divider type='vertical' />
          <a-dropdown>
            <a class='ant-dropdown-link'>更多 <a-icon type='down' /></a>
            <a-menu slot='overlay'>
              <a-menu-item>
                <a-popconfirm title='确定删除吗?' @confirm='() => handleDelete(record.id)'>
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>
    <!-- table区域-end -->

    <!-- 表单区域 -->
    <visitor-modal ref='modalForm' @ok='modalFormOk'></visitor-modal>
  </a-card>
</template>

<script>
import VisitorModal from './modules/VisitorModal'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { getAction } from '@api/manage'
import { filterObj } from '@/utils/util'
import { getProjectList } from '@api/api'

export default {
  name: 'VisitorList',
  mixins: [JeecgListMixin],
  components: {
    VisitorModal
  },
  data() {
    return {
      description: '管理页面',
      projectList:[],
      userList:[],
      // 表头
      columns: [
        {
          title: '项目名称',
          width: 200,
          fixed:'left',
          align: 'center',
          dataIndex: 'projectName'
        },
        {
          title: '业主姓名',
          fixed:'left',
          width: 150,
          align: 'center',
          dataIndex: 'userName'
        },
        {
          title: '访客姓名',
          fixed:'left',
          width: 150,
          align: 'center',
          dataIndex: 'visitName'
        },
        {
          title: '访客手机号',
          width: 150,
          align: 'center',
          dataIndex: 'visitMobile'
        },
        {
          title: '访客身份证',
          width: 200,
          align: 'center',
          dataIndex: 'cardNum'
        },

        {
          title: '登记类型',
          width: 150,
          align: 'center',
          dataIndex: 'registerType',
          customRender: (record) => {
            if (record == '' || record == null) {
              return
            }
            if (record == '1') {
              return '微信小程序'
            } else {
              return '线下登记'
            }
          }
        },
        {
          title: '来访类型',
          align: 'center',
          width: 150,
          dataIndex: 'sourceType',
          customRender: (record) => {
            if (record == '' || record == null) {
              return
            }
            if (record == '1') {
              return '业主邀请'
            } else {
              return '亲友来访'
            }
          }
        },
        {
          title: '逾期处理',
          align: 'center',
          width: 200,
          dataIndex: 'overdue',
          customRender: (record) => {
            if (record == '' && record == null) {
              return
            }
            if (record == '1') {
              return '可以通行'
            } else {
              return '不可通行'
            }
          }
        },
        {
          title: '访问房屋',
          width: 400,
          align: 'center',
          dataIndex: 'visitHouseName'
        },


        {
          title: '访客车牌号',
          width: 150,
          align: 'center',
          dataIndex: 'visitVehicleNo'
        },
        {
          title: '访客类型',
          width: 150,
          align: 'center',
          dataIndex: 'visitType',
          customRender: (record) => {
            if (record == '' || record == null) {
              return
            }
            if (record == '1') {
              return '亲朋'
            } else {
              return '同事'
            }
          }
        },
        {
          title: '随行人数',
          width: 100,
          align: 'center',
          dataIndex: 'visitPeopleNum'
        },


        {
          title: '备注',
          width: 150,
          align: 'center',
          dataIndex: 'remark'
        },
        {
          title: '到访开始时间',
          width: 200,
          align: 'center',
          dataIndex: 'startTime'
        },
        {
          title: '到访结束时间',
          width: 200,
          align: 'center',
          dataIndex: 'endTime'
        },


        {
          title: '操作',
          fixed:'right',
          width: 200,
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/visitor/list',
        delete: '/visitor/delete',
        deleteBatch: '/visitor/deleteBatch',
        exportXlsUrl: '/visitor/exportXls',
        getUserList:'/user/user/getUserByProjectId',
        importExcelUrl: '/visitor/importExcel'
      }
    }
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  created() {
    this.initProjectList()
    this.keyupEnter()
  },
  methods: {
    keyupEnter(){
      this.loadData(1)
    },
    initUserList(e){
      this.queryParam.userName=""
      this.userList=[]
      getAction(this.url.getUserList,{id:e}).then((res)=>{
        if (res.success){
          if (res.result!=null){
            debugger
            this.userList = res.result
          }
        }
      })

    },
    initProjectList() {
      this.userList=[]
      getProjectList().then((res)=>{
        if(res.success){
          this.projectList = res.result
        }
      })
    },
    filterOption(input, option) {
      input = String(input)
      return (
        option.componentOptions.children[0].text.toUpperCase().indexOf(input.toUpperCase()) >= 0
      )
    },
    onChangeStartTime(date, dateString) {
      this.queryParam.opTime_begin = dateString
    },

    searchQuery() {
      let startTime = this.queryParam.opTime_begin
      let endTime = this.queryParam.opTime_end

      if (startTime != '' && endTime != '') {
        if (startTime > endTime) {
          this.$message.error('开始时间不能大于结束时间')
          return
        }
      }

      if (startTime == '' && endTime != '') {
        this.$message.error('请选择开始时间')
        return
      }

      if (startTime != '' && endTime == '') {
        this.$message.error('请选择结束时间')
        return
      }

      this.loadData(1)
    },
    getQueryParams() {
      let param = Object.assign({}, this.isorter, this.queryParam)
      param.field = this.getQueryField()
      param.pageNo = this.ipagination.current
      param.pageSize = this.ipagination.pageSize

      return filterObj(param)
    },
    loadData(arg) {
      if (!this.url.list) {
        this.$message.error('请设置url.list属性!')
        return
      }
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1
      }
      var params = this.getQueryParams()//查询条件

      console.log('查询条件：' + params)
      this.loading = true
      getAction(this.url.list, params).then((res) => {
        if (res.success) {
          if (res.result != null) {
            this.dataSource = res.result.records
            console.log(this.dataSource)
            this.ipagination.total = res.result.total
          }
        }
        if (res.code === 510) {
          this.$message.warning(res.message)
        }
        this.loading = false
      })
    },
    onChangeEndTime(date, dateString) {
      this.queryParam.opTime_end = dateString
    },
    handleAdd: function() {
      this.$refs.modalForm.add()
      this.$refs.modalForm.title = '新增'
      this.$refs.modalForm.disableSubmit = false
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>